<?php

namespace application\admin\controller;

use ticky\request;

class sitemap extends base {

    function __construct() {
        parent::__construct();
        $this->data = array();
        $this->filename = 'sitemap.xml';
        $this->header = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
        $this->footer = '</urlset>' . "\n";
    }

    // 首页
    public function index() {
        $is_make_xml = $is_make_txt = false;
        $make_xml_time = $make_txt_time = 0;
        if (is_file(ROOT_PATH . 'sitemap.xml')) {
            $is_make_xml = true;
            $make_xml_time = date('Y-m-d H:i:s', filectime(ROOT_PATH . 'sitemap.xml'));
        }
        if (is_file(ROOT_PATH . 'sitemap.txt')) {
            $is_make_txt = true;
            $make_txt_time = date('Y-m-d H:i:s', filectime(ROOT_PATH . 'sitemap.txt'));
        }
        $modelinfo = get_modelinfo();
        $this->assign('is_make_xml', $is_make_xml);
        $this->assign('make_xml_time', $make_xml_time);
        $this->assign('is_make_txt', $is_make_txt);
        $this->assign('make_txt_time', $make_txt_time);
        $this->assign('modelinfo', $modelinfo);
        $this->display('sitemap/index');
    }

    /**
     * 生成地图
     */
    public function make_sitemap() {
        if (isset($_POST['dosubmit'])) {
            $model = isset($_POST['model']) ? $_POST['model'] : array(1);
            $limit = isset($_POST['limit']) ? intval($_POST['limit']) : 100;
            $changefreq = isset($_POST['changefreq']) ? safe_replace($_POST['changefreq']) : 'weekly';
            $priority = isset($_POST['priority']) ? $_POST['priority'] : 0.8;
            $type = isset($_POST['type']) ? intval($_POST['type']) : 1;

            // 生成地图头部　第一条
            $item = $this->_sitemap_item(SITE_URL, TIMESTAMP, 'daily', '1.0');
            $this->_add_data($item);

            foreach ($model as $m) {
                $data = db('content')->field('updatetime,url')->where(array('status' => '1', 'modelid' => $m))->order('contentid DESC')->limit($limit)->select();
                foreach ($data as $val) {
                    $item = $this->_sitemap_item($val['url'], $val['updatetime'], $changefreq, $priority);
                    $this->_add_data($item);
                }
            }

            if (!$type) {
                $str = $this->_xml_format();
            } else {
                $str = $this->_txt_format();
                $this->filename = 'sitemap.txt';
            }
            $strlen = @file_put_contents(ROOT_PATH . $this->filename, $str);

            if ($strlen) {
                showmsg('生成文件 ' . $this->filename . ' 成功！');
            } else {
                showmsg('生成文件 ' . $this->filename . ' 失败，请检查是否有写入权限！', 'stop');
            }
        }
    }

    /**
     * 生成xml格式
     */
    private function _xml_format() {
        $str = $this->header;
        foreach ($this->data as $val) {
            $str .= "<url>\n\t<loc>" . $val['loc'] . "</loc>\n";
            $str .= "\t<lastmod>" . date('Y-m-d', $val['lastmod']) . "</lastmod>\n";
            $str .= "\t<changefreq>" . $val['changefreq'] . "</changefreq>\n";
            $str .= "\t<priority>" . $val['priority'] . "</priority>\n";
            $str .= "</url>\n";
        }
        return $str . $this->footer;
    }

    /**
     * 生成txt格式
     */
    private function _txt_format() {
        $str = '';
        foreach ($this->data as $val) {
            $str .= $val['loc'] . "\n";
        }
        return $str;
    }

    /**
     * 添加数据
     */
    private function _add_data($new_item) {
        $this->data[] = $new_item;
    }

    /**
     * 创建地图格式
     */
    private function _sitemap_item($loc, $lastmod = '', $changefreq = '', $priority = '') {
        $data = array();
        $data['loc'] = $loc;
        $data['lastmod'] = $lastmod;
        $data['changefreq'] = $changefreq;
        $data['priority'] = $priority;
        return $data;
    }

}
